﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.Entity;
using CMN.Common;
using CMN.Framework;
using CMN.BLL;
namespace CMN.UI
{
    /// <summary>
    /// 标签解析
    /// </summary>
    public class Normal_TagsDisplay
    {
        static Normal_TagsDisplay()
        {
            BaseTemplateDisplay.OnNVelocityLoadPath += new EventHandler<TemplateArg>(TagsLoadPath);
            BaseTemplateDisplay.OnAfterBaseDisplay += new EventHandler<TemplateDisplayArg>(TagsList);
            BaseTemplateDisplay.OnOtherPageDisplay += new EventHandler<TemplateDisplayArg>(TagDisplay);
        }

        /// <summary>
        /// 绑定模板
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void TagDisplay(object o, TemplateDisplayArg arg)
        {
            if (arg.PageHelper.OtherTypeName == "Tags")
            {
                Normal_TagsEntity tagEntity = MvcWebCacheManager.GetNormal_Tags(arg.PageHelper.ContentId);
                if (tagEntity != null)
                {
                    arg.PageHelper.Title = (string.IsNullOrEmpty(tagEntity.SeoTagName) ? tagEntity.TagName : tagEntity.SeoTagName);
                    arg.PageHelper.SearchWords = tagEntity.TagName;
                    arg.PageHelper.Description = tagEntity.HtmlInfo;
                    arg.NvelocityTemplate.Put("tagEntity", tagEntity);

                }
                else
                {
                    tagEntity = new Normal_TagsEntity();
                    arg.PageHelper.Title = tagEntity.TagName;
                    arg.PageHelper.Description = tagEntity.HtmlInfo;
                    arg.NvelocityTemplate.Put("tagEntity", tagEntity);
                    AppLog.Write("TagDisplay:" + arg.PageHelper.ContentId + "为空 ", AppLog.LogMessageType.Debug);
                }

            }
        }

        /// <summary>
        /// 加载模板路径
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void TagsLoadPath(object o, TemplateArg arg)
        {
            if (arg.PageHelper.OtherTypeName == "Tags")
            {

                arg.TemplateEntity = MvcWebCacheManager.GetDefaultTemlateEntity();

                arg.TemplateEntity.TemplateName = "tag.htm";
            }

        }
        /// <summary>
        /// 绑定模板
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void TagsList(object o, TemplateDisplayArg arg)
        {
            Normal_TagsDisplay tag = new Normal_TagsDisplay();
            arg.NvelocityTemplate.Put("tag", tag);

        }


        /// <summary>
        /// 获取标签列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public List<Normal_TagsEntity> List(string channelPath, int pageSize)
        {

            List<Normal_TagsEntity> noticeList = MvcWebCacheManager.GetNormal_TagsList();
            List<Normal_TagsEntity> rnoticeList = new List<Normal_TagsEntity>();
            foreach (Normal_TagsEntity tag in noticeList)
            {
                if (rnoticeList.Count < pageSize)
                {
                    if (string.IsNullOrEmpty(channelPath) || tag.ChannelPath.StartsWith(channelPath))
                    {
                        rnoticeList.Add(tag);
                    }
                }
                else
                    break;
            }
            return rnoticeList;
        }

        /// <summary>
        /// 获取推荐标签列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public List<Normal_TagsEntity> List(string channelPath, bool isRecommend, int pageSize)
        {

            List<Normal_TagsEntity> noticeList = MvcWebCacheManager.GetNormal_TagsList();
            List<Normal_TagsEntity> rnoticeList = new List<Normal_TagsEntity>();
            foreach (Normal_TagsEntity tag in noticeList)
            {
                if (rnoticeList.Count < pageSize)
                {
                    if ((string.IsNullOrEmpty(channelPath) || tag.ChannelPath.StartsWith(channelPath)) && tag.IsRecommend == 1)
                    {
                        rnoticeList.Add(tag);
                    }
                }
                else
                    break;
            }
            return rnoticeList;
        }
        /// <summary>
        /// 获取推荐标签列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public List<Normal_TagsEntity> List(string channelPath, bool isRecommend, int orderNum, int pageSize)
        {

            List<Normal_TagsEntity> noticeList = MvcWebCacheManager.GetNormal_TagsList();
            List<Normal_TagsEntity> rnoticeList = new List<Normal_TagsEntity>();
            foreach (Normal_TagsEntity tag in noticeList)
            {
                if (rnoticeList.Count < pageSize)
                {
                    if (tag.OrderNum == orderNum && (string.IsNullOrEmpty(channelPath) || tag.ChannelPath.StartsWith(channelPath)) && (!isRecommend || tag.IsRecommend == 1))
                    {
                        rnoticeList.Add(tag);
                    }
                }
                else
                    break;
            }
            return rnoticeList;
        }


        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public List<Normal_TagsEntity> List(string strId)
        {

            List<Normal_TagsEntity> noticeList = MvcWebCacheManager.GetNormal_TagsList();
            List<Normal_TagsEntity> rnoticeList = new List<Normal_TagsEntity>();
            if (!string.IsNullOrEmpty(strId))
            {
                string[] idList = strId.Split(',');
                for (int i = 0; i < idList.Length; i++)
                {
                    foreach (Normal_TagsEntity tag in noticeList)
                    {

                        if (tag.Id.ToString() == idList[i])
                        {
                            rnoticeList.Add(tag);
                            break;
                        }

                    }
                }
            }
            return rnoticeList;
        }
    }

}
